package com.ruoyi.project.channel.channelAccount.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.AddressUtils;
import com.ruoyi.common.utils.SMSUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.channelAccount.mapper.ChannelAccountMapper;
import com.ruoyi.project.channel.channelAccount.domain.ChannelAccount;
import com.ruoyi.project.channel.channelAccount.service.IChannelAccountService;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;

/**
 * 通道账户 服务层实现
 *
 * @author ruoyi
 * @date 2019-04-09
 */
@Service
public class ChannelAccountServiceImpl implements IChannelAccountService {
    @Autowired
    private ChannelAccountMapper channelAccountMapper;
    @Autowired
    private IUserService userService;
    /**
     * 查询通道账户信息
     *
     * @param accountId 通道账户ID
     * @return 通道账户信息
     */
    @Override
    public ChannelAccount selectChannelAccountById(String accountId) {
        return channelAccountMapper.selectChannelAccountById(accountId);
    }

    /**
     * 查询通道账户列表
     *
     * @param channelAccount 通道账户信息
     * @return 通道账户集合
     */
    @Override
    public List<ChannelAccount> selectChannelAccountList(ChannelAccount channelAccount) {
        return channelAccountMapper.selectChannelAccountList(channelAccount);
    }

    /**
     * 新增通道账户
     *
     * @param channelAccount 通道账户信息
     * @return 结果
     */
    @Override
    public int insertChannelAccount(ChannelAccount channelAccount) {
        channelAccount.setCreateBy(ShiroUtils.getLoginName());
        channelAccount.setParentId(ShiroUtils.getUserId().toString());
        Date date = new Date(); //获取当前的系统时间。
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm") ; //使用了默认的格式创建了一个日期格式化对象。
        String time = dateFormat.format(date);
        String ip = ServletUtils.getRequest().getRemoteHost();
        String address = "XX XX";
        try {
            address=AddressUtils.getRealAddressByIP(ip);
        }catch (Exception e){
        }
        String content ="您的账号:"+ ShiroUtils.getLoginName() +"于"+time+"在"+ address+"新建了账号"+channelAccount.getAccount()+",ip地址："+ip;
        if(ShiroUtils.getSysUser().getPhonenumber() !=null && !"".equals(ShiroUtils.getSysUser().getPhonenumber().trim())){
//            JSONObject jsonObject = SMSUtil.sendSms(content,ShiroUtils.getSysUser().getPhonenumber());
        }
        return channelAccountMapper.insertChannelAccount(channelAccount);
    }

    /**
     * 修改通道账户
     *
     * @param channelAccount 通道账户信息
     * @return 结果
     */
    @Override
    public int updateChannelAccount(ChannelAccount channelAccount) {
//        channelAccount.setUpdateBy(ShiroUtils.getLoginName());
        ChannelAccount oldAccount = selectChannelAccountById(channelAccount.getAccountId());
        User user = userService.selectUserById(oldAccount.getChannelUserId());
        if(!oldAccount.getAccount().equals(channelAccount.getAccount())
                || !oldAccount.getAppid().equals(channelAccount.getAppid())){
            // 几个关键点被修改要发短信通知
            Date date = new Date(); //获取当前的系统时间。
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm") ; //使用了默认的格式创建了一个日期格式化对象。
            String time = dateFormat.format(date);
            String ip = ServletUtils.getRequest().getRemoteHost();
            String address = "XX XX";
            try {
                address=AddressUtils.getRealAddressByIP(ip);
            }catch (Exception e){
            }
            String content ="您的账号:"+ user.getLoginName() +"于"+time+"在"+ address+"修改了账号"+channelAccount.getAccount()+",ip地址:"+ip+"";
            if(user.getPhonenumber() !=null && !"".equals(user.getPhonenumber().trim())){
//                JSONObject jsonObject = SMSUtil.sendSms(content,user.getPhonenumber());
            }
        }

        return channelAccountMapper.updateChannelAccount(channelAccount);
    }

    /**
     * 删除通道账户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteChannelAccountByIds(String ids) {
        return channelAccountMapper.deleteChannelAccountByIds(Convert.toStrArray(ids));
    }

    /**
     * 检查账号唯一性
     *
     * @param account
     * @return
     */
    public boolean checkAccountUnique(String account,String payType){
        List<ChannelAccount> accounts = channelAccountMapper.selectChannelAccountListByAccount(account,payType);
        if(accounts.size() == 0){
            return  true;
        }
        return  false;
    }

    public int setAmount(ChannelAccount channelAccount){
        return channelAccountMapper.setAmount(channelAccount);
    }
}
